<template>
  <div class>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>分类参数</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card class="mt15">
      <!-- 警告框 -->
      <el-alert title="注意：只允许为第三级分类设置相关参数" type="warning" show-icon></el-alert>
      <!-- 级联菜单 -->
      <el-row class="t-left">
        <el-col :span="8">
          选择商品分类:
          <el-cascader
            v-model="selectedCates"
            :options="allCates"
            :props="selectCateProp"
            @change="inputSelectChange"
            class="width100"
          ></el-cascader>
        </el-col>
      </el-row>

      <!-- 选项卡/tab栏 -->
      <el-tabs v-model="activeName" @tab-click="handleTabClick" class="t-left">
        <!-- 动态参数tab -->
        <el-tab-pane label="动态参数" name="many">
          <el-button type="primary" size="mini" :disabled="isWork" @click="showAddParaDia">添加动态参数</el-button>
          <!-- 动态参数表格 -->
          <el-table :data="tableManyData" style="width: 100%;margin-top:15px;" stripe border>
            <el-table-column type="expand">
              <template slot-scope="scope">
                <!-- {{scope.row.attr_vals}} -->
                <el-tag
                  v-for="(item,i) in scope.row.attr_vals"
                  :key="i"
                  closable
                  @close="deleteParams(scope.row,i)"
                >{{item}}</el-tag>
                <!-- 可新增的tag标签 -->
                <el-input
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="$event.target.blur"
                  @blur="handleInputConfirm(scope.row)"
                  class="width100"
                ></el-input>
                <el-button v-else size="small" @click="showInput(scope.row)">+ New Tag</el-button>
              </template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="attr_name" label="参数名称" width="180"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="deleteCate(scope.row.attr_id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>

        <!-- 静态属性tab -->
        <el-tab-pane label="静态属性" name="only">
          <el-button type="primary" size="mini" :disabled="isWork" @click="showAddParaDia">添加静态属性</el-button>
          <!-- 静态属性表格 -->
          <el-table :data="tableOnlyData" style="width: 100%;margin-top:15px;" stripe border>
            <el-table-column type="expand">
              <template slot-scope="scope">{{scope.row.attr_vals}}</template>
            </el-table-column>
            <el-table-column type="index"></el-table-column>
            <el-table-column prop="attr_name" label="属性名称" width="180"></el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  size="mini"
                  @click="deleteCate(scope.row.attr_id)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 添加属性/参数对话框 -->
    <el-dialog :title="'添加'+curTitle" :visible.sync="addParadialogVisible" width="30%">
      <!-- 添加表单 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="120px">
        <el-form-item :label="'添加'+curTitle" prop="attr_name">
          <el-input v-model="addForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addParadialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitAddPara">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  components: {},
  data() {
    return {
      // 级联选中存的值
      selectedCates: [1, 3, 6],
      // 所有的分类列表
      allCates: [],
      // 级联配置项
      selectCateProp: {
        label: "cat_name",
        value: "cat_id",
        children: "children",
        expandTrigger: "hover",
      },
      // tab栏当前页
      activeName: "many",
      // only的数据存放
      tableOnlyData: [],
      // many的数据存放
      tableManyData: [],
      // 控制添加参数/属性的对话框显示隐藏
      addParadialogVisible: false,
      // 添加参数表单数据
      addForm: {
        attr_name: "",
      },
      // 添加参数表单规则
      addFormRules: {},
    };
  },
  created() {
    this.getAllCates();
  },
  mounted() {},
  watch: {},
  computed: {
    isWork() {
      return this.selectedCates.length != 3;
    },
    // 分类id
    cateId() {
      if (this.selectedCates.length != 3) {
        return;
      }
      return this.selectedCates[this.selectedCates.length - 1];
    },
    // 动态标题
    curTitle(){
      return this.activeName=='only'?'静态属性':'动态参数'
    },
  },
  methods: {
    // 获取所有分类列表
    getAllCates() {
      this.$axios.get("categories?type=3").then((val) => {
        // console.log(val);
        if (val.meta.status != 200) {
          return this.$message.error(val.meta.msg);
        }
        this.allCates = val.data;
      });
    },
    // 发请求
    getParams() {
      this.$axios
        .get("categories/" + this.cateId + "/attributes", {
          params: {
            sel: this.activeName,
          },
        })
        .then((val) => {
          console.log(val);
          if (val.meta.status != 200) {
            return this.$message.error("请先选择分类!");
          }

          // 将得到的数据中的attr_vals的值从字符串用空格拆分成数组
          val.data.forEach((item) => {
            // 去掉两边空格
            item.attr_vals = item.attr_vals.trim();
            // 给每个参数数据单独绑定显示状态和值
            item.inputVisible = false;
            item.inputValue = "";
            if (this.activeName == "many") {
              item.attr_vals =
                item.attr_vals.length == 0 ? [] : item.attr_vals.split(" ");
              return;
            }
            item.attr_vals = item.attr_vals;
          });

          // 判断activeName 是only还是many
          if (this.activeName == "only") {
            this.tableOnlyData = val.data;
          } else {
            this.tableManyData = val.data;
          }
        });
    },
    // 级联点选时
    inputSelectChange() {
      // console.log(this.selectedCates);
      this.getParams();
    },
    // tab栏点击时
    handleTabClick() {
      // console.log(this.activeName);
      this.getParams();
    },
    // 点击new tag按钮时
    showInput(row) {
      console.log(row);
      row.inputVisible = true;
      // 自动获取焦点
      this.$nextTick((_) => {
        this.$refs.saveTagInput.focus();
      });
    },
    // 点击tag标签的x时
    deleteParams(row, i) {
      console.log(row.attr_vals);
      row.attr_vals.splice(i, 1);
      // row.attr_vals = row.attr_vals.join(" ");
      // 点击tag标签的x删除当前那一个tag
      this.$axios
        .put("categories/" + this.cateId + "/attributes/" + row.attr_id, {
          attr_name: row.attr_name,
          attr_sel: this.activeName,
          attr_vals: row.attr_vals.join(" "),
        })
        .then((val) => {
          console.log(val);
          console.log(val);
          row.attr_vals = val.data.attr_vals.split(" ");
        });
    },
    // 在新增tag按钮处失焦，点击enter的时候
    handleInputConfirm(row) {
      // 提交请求把新的参数给到attr_vals值
      // console.log(row);
      row.attr_vals.push(row.inputValue);
      // row.attr_vals=row.attr_vals.join(' ');
      this.$axios
        .put("categories/" + this.cateId + "/attributes/" + row.attr_id, {
          attr_name: row.attr_name,
          attr_sel: this.activeName,
          attr_vals: row.attr_vals.join(" "),
        })
        .then((val) => {
          console.log(val);
          row.attr_vals = val.data.attr_vals.split(" ");
        });
      row.inputValue = "";
      // 将按钮显示，输入框隐藏
      row.inputVisible = false;
    },

    // 点击添加参数/属性的按钮时
    showAddParaDia() {
      this.addParadialogVisible = true;
    },
    // 提交添加参数/属性框
    submitAddPara() {
      // 发请求添加参数
      this.$axios
        .post("categories/" + this.cateId + "/attributes", {
          attr_name: this.addForm.attr_name,
          attr_sel: this.activeName,
        })
        .then((val) => {
          console.log(val);
          if (val.meta.status != 201) {
            return this.$message.error(val.meta.msg);
          }
          this.getParams();
        });

      this.addParadialogVisible = false;
    },
    // 点击删除参数按钮
    deleteCate(attrId) {
      console.log(attrId);
      // 发请求删参数
      this.$axios
        .delete("categories/" + this.cateId + "/attributes/" + attrId)
        .then((val) => {
          console.log(val);
          if (val.meta.status != 200) {
            return this.$message.error(val.meta.msg);
          }
          this.getParams();
        });
    },
  },
};
</script>
<style lang="css" scoped>
.mt15 {
  margin-top: 15px;
}
.t-left {
  text-align: left;
  margin-top: 15px;
}
.el-tag {
  margin: 5px;
}
.width100 {
  width: 150px;
}
</style>